﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DevExpress.Web.ASPxClasses;
using DevExpress.Web.ASPxEditors;
using RR;

public partial class Default2 : System.Web.UI.Page
{
    private RegistRar regrar = new RegistRar();

    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Callback_Callback(object source, DevExpress.Web.ASPxCallback.CallbackEventArgs e)
    {
        Exporter.WriteXlsxToResponse();
    }

    private void CreateSchet()
    {
        try
        {
            int code = Convert.ToInt32(grdPayment.GetRowValues(grdPayment.FocusedRowIndex, "CODE"));
            if (code == 0)
            {
                throw new Exception("Не указан шаблон платежа! установите курсор на нужную запись.");
            }
            DateTime sDate = dtSchet.Date;
            int sNum = Convert.ToInt32(snomer.Value);
            int sKontr = Convert.ToInt32(grdPayment.GetRowValues(grdPayment.FocusedRowIndex, "KONTRAGENT"));
            int sDog = Convert.ToInt32(grdPayment.GetRowValues(grdPayment.FocusedRowIndex, "DOG"));
            Decimal sTarif = Convert.ToDecimal(grdPayment.GetRowValues(grdPayment.FocusedRowIndex, "TARIF"));
            Decimal sQuant = Convert.ToDecimal(grdPayment.GetRowValues(grdPayment.FocusedRowIndex, "QUANTITY"));
            int sService = Convert.ToInt32(grdPayment.GetRowValues(grdPayment.FocusedRowIndex, "SERVICES"));
            #region создаем заголовок счета

            SCHET schet = SCHET.CreateSCHET(regrar);
            schet.DOG = sDog;
            schet.KONTRAGENT = sKontr;
            schet.SDATA = sDate;
            schet.AUTO = 1;
            schet.SNOMER = sNum;
            schet.NOTE = String.Format("создан на основании шаблона №{0}", code);
            #endregion
            #region создание спецификации счета
            SCHETSPEC schetspec = SCHETSPEC.CreateSCHETSPEC(regrar);
            schetspec.QUANTITY = sQuant;
            schetspec.SCHET = schet.CODE;
            schetspec.TARIF = sTarif;
            schetspec.SERVICES = sService;
            #endregion

            regrar.SaveChanges();
        }
        catch (Exception ex)
        {

            throw new Exception(ex.InnerException.Message);
        }
    }

    protected void grdPayment_RowInserting(object sender, DevExpress.Web.Data.ASPxDataInsertingEventArgs e)
    {
        if (e.NewValues["CODE"] == null)
            e.NewValues["CODE"] = regrar.Gen_id("SRVSHABLON_gen");
    }
    protected void grdPayment_CellEditorInitialize(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewEditorEventArgs e)
    {
        if (e.Column.FieldName == "DOG")
        {
            var combo = (ASPxComboBox)e.Editor;
            combo.Callback += new CallbackEventHandlerBase(cmbDog_OnCallback);

            var grid = e.Column.Grid;
            if (!combo.IsCallback)
            {
                string kontr = "-1";
                if (!grid.IsNewRowEditing)
                    kontr = Convert.ToString(grid.GetRowValues(e.VisibleIndex, "KONTRAGENT"));
                FillDogCombo(combo, kontr);
            }
        }
    }

    private void FillDogCombo(ASPxComboBox combo, string kontr)
    {
        var k = -1;
        Int32.TryParse(kontr, out k);
        if (k < 0) { combo.Items.Clear(); return; }

        ds_dog.Where = "it.KONTRAGENT = @kontr";
        ds_dog.WhereParameters.Clear();
        ds_dog.WhereParameters.Add("kontr", DbType.Int32, kontr);
        combo.DataBind();
    }

    private void cmbDog_OnCallback(object sender, CallbackEventArgsBase e)
    {
        FillDogCombo(sender as ASPxComboBox, e.Parameter);
    }

    protected void btnOk_Click(object sender, EventArgs e)
    {
        CreateSchet();
        Response.Redirect("~/calc/payments.aspx");
    }
}